package com.sakiko.audiobook.account.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sakiko.audiobook.model.account.UserAccount;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;

@Mapper
public interface UserAccountMapper extends BaseMapper<UserAccount> {

    /**
     * 检查扣减金额是否成功
     *
     * @param userId
     * @param amount
     * @return
     */
    @Update("update user_account\n" +
            "set available_amount = available_amount - #{amount},\n" +
            "    total_amount     = total_amount - #{amount},\n" +
            "    total_pay_amount = total_pay_amount + #{amount}\n" +
            "where user_id = #{userId}\n" +
            "  and available_amount >= #{amount} and is_deleted = 0;")
    int checkAndDeduct(@Param("userId") Long userId,@Param("amount") BigDecimal amount);
}
